<template>
  <el-dialog :model-value="dialogData.isShow" :title="dialogData.id?'编辑':'新增'"
             @close="closeDialog" draggable>
    <el-form :model="form" ref="formRef" :rules="rules" label-width="90px">
      <el-row :gutter="10">
         <el-col :span="12">
           <el-form-item label="邀请人id" prop="inviteId">
             <el-input v-model="form.inviteId" maxlength="100" placeholder="请输入邀请人id"/>
           </el-form-item>
         </el-col>
         <el-col :span="12">
           <el-form-item label="手机号" prop="username">
             <el-input v-model="form.username" maxlength="255" placeholder="请输入手机号"/>
           </el-form-item>
         </el-col>
      </el-row>
      <el-row :gutter="10">
         <el-col :span="12">
           <el-form-item label="微信名" prop="nickname">
             <el-input v-model="form.nickname" maxlength="255" placeholder="请输入微信名"/>
           </el-form-item>
         </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-input type="number" v-model="form.status" placeholder="请输入状态"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="累计消费" prop="balance">
            <el-input type="number" v-model="form.balance" placeholder="请输入累计消费"/>
          </el-form-item>
        </el-col>
         <el-col :span="12">
           <el-form-item label="updatedTime">
             <el-date-picker v-model="form.updatedTime" type="datetime" placeholder="请选择updatedTime"/>
           </el-form-item>
         </el-col>
      </el-row>
      <el-row :gutter="10">
         <el-col :span="12">
           <el-form-item label="头像" prop="avatarUrl">
             <el-input v-model="form.avatarUrl" maxlength="255" placeholder="请输入头像"/>
           </el-form-item>
         </el-col>
         <el-col :span="12">
           <el-form-item label="password" prop="password">
             <el-input v-model="form.password" maxlength="255" placeholder="请输入password"/>
           </el-form-item>
         </el-col>
      </el-row>
      <el-row :gutter="10">
         <el-col :span="12">
           <el-form-item label="openid" prop="openid">
             <el-input v-model="form.openid" maxlength="255" placeholder="请输入openid"/>
           </el-form-item>
         </el-col>
        <el-col :span="12">
          <el-form-item label="isDel" prop="isDel">
            <el-input type="number" v-model="form.isDel" placeholder="请输入isDel"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="等级id" prop="levelId">
            <el-input type="number" v-model="form.levelId" placeholder="请输入等级id"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import {ElMessage, FormInstance, FormRules} from "element-plus";
import {addMember, getMember, updateMember} from "@/api/shop/member";

const formRef = ref<FormInstance>()

const emits = defineEmits<{
  (event: 'refresh'): void
}>()

// 表单
let form: any = ref({
    id: null,
    inviteId: null,
    username: null,
    nickname: null,
    status: null,
    balance: null,
    updatedTime: null,
    avatarUrl: null,
    password: null,
    openid: null,
    isDel: null,
    levelId: null,
});

// 效验规则
const rules = reactive<FormRules>({
})

// 获取详情
const getDetails = (id: string) => {
  getMember(id).then(res => {
    form.value = Object.assign({}, form.value, res);
  })
}

// 弹框数据
const dialogData = reactive({
  isShow: false,
  id: null
})

// 打开弹框
const openDialog = async (id: string) => {
  dialogData.isShow = true;
  dialogData.title = '新增用户信息';
  if (id) {
    dialogData.id = id;
    dialogData.title = '编辑用户信息';
    getDetails(id);
  }
}

// 关闭弹框
const closeDialog = () => {
  dialogData.isShow = false;
  form.value = {};
}
// 提交

const submit = async () => {
   if (!formRef.value) return;
   await formRef.value.validate((valid: any) => {
   if (valid) {
       let data = form.value;
     if (data.id) {
       updateMember({...data}).then(() => {
         ElMessage.success('操作成功');
         closeDialog();
         emits('refresh');
       })
     } else {
       addMember({...data}).then(() => {
       ElMessage.success('操作成功');
       closeDialog();
       emits('refresh');
       })
     }
   }
  })
}

defineExpose({
  openDialog,
});
</script>

<style scoped>

</style>
